<?php
/**
 * StatZone default controller
 * 
 * @package    USS.StatZone
 * @subpackage Components
 * @link www.ugenix.co.za www.statzone.co.za
 * @license		Commercial
 * @author Salie Hendricks
 */


// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.view' );

/**
 * Players View
 *
 * @package    USS.StatZone
 * @subpackage Components
 */
class StatZoneViewPlayers extends JView
{

    function setPlayersToolbar()
	{
		JToolBarHelper::title(   JText::_( 'StatZone Players Manager' ), 'generic.png' );
        JToolBarHelper::deleteList();
		JToolBarHelper::editListX();
		JToolBarHelper::addNewX();
	}
    
    function setSinglePlayerToolbar(&$row)
	{
		$task = JRequest::getVar( 'task', '', 'method', 'string');

		JToolBarHelper::title( $task == 'add' ? JText::_( 'Player' ) . ': <small><small>[ '. JText::_( 'New' ) .' ]</small></small>' : JText::_( 'Player' ) . ': <small><small>[ '. JText::_( 'Edit ['.$row->id.']' ) .' ]</small></small>', 'generic.png' );
		JToolBarHelper::save( 'save' );
		JToolBarHelper::apply('apply');
		JToolBarHelper::cancel( 'cancel' );
		
	}
    
	function singlePlayer( &$row, &$lists)
	{
		StatZoneViewPlayers::setSinglePlayerToolbar($row);
		JRequest::setVar( 'hidemainmenu', 1 );
        $contentlink		= JRoute::_( 'index.php?option=com_content&sectionid=-1&task=edit&cid[]='. $row->contentid );
        
		//JFilterOutput::objectHTMLSafe( $row, ENT_QUOTES, 'customclubcode' );
		?>
		<script language="javascript" type="text/javascript">
		<!--
		
		function submitbutton(pressbutton) {
			var form = document.adminForm;
			if (pressbutton == 'cancel') {
				submitform( pressbutton );
				return;
			}
			// do field validation
			if (form.firstname.value == "") {
				alert( "<?php echo JText::_( 'You must provide a first name.', true ); ?>" );
			}
          else if (form.surname.value == "") {
				alert( "<?php echo JText::_( 'You must provide a surname.', true ); ?>" );
			}             
			else if ( getSelectedValue('adminForm','clubid') == 0 ) {
				alert( "<?php echo JText::_( 'Please select a club.', true ); ?>" );
			} 
          else {
				submitform( pressbutton );
			}
		}
		//-->
		</script>
		<form action="index.php" method="post" name="adminForm">

		<div class="col100">
			<fieldset class="adminform">
				<legend><?php echo JText::_( 'Details' ); ?></legend>

				<table class="admintable">
				<tbody>
					<tr>
						<td width="20%" class="key">
							<label for="firstname">
								<?php echo JText::_( 'First name' ); ?>:
							</label>
						</td>
						<td width="80%">
							<input class="inputbox" type="text" name="firstname" id="firstname" size="50" value="<?php echo $row->firstname;?>" />
						</td>
					</tr>
                    <tr>
						<td width="20%" class="key">
							<label for="surname">
								<?php echo JText::_( 'Surname' ); ?>:
							</label>
						</td>
						<td width="80%">
							<input class="inputbox" type="text" name="surname" id="surname" size="50" value="<?php echo $row->surname;?>" />
						</td>
					</tr>
                    <tr>
						<td width="20%" class="key">
							<label for="idnumber">
								<?php echo JText::_( 'ID number' ); ?>:
							</label>
						</td>
						<td width="80%">
							<input class="inputbox" type="text" name="idnumber" id="idnumber" size="50" value="<?php echo $row->idnumber;?>" />
						</td>
					</tr>
                    <tr>
						<td width="20%" class="key">
							<label for="alias">
								<?php echo JText::_( 'Email address' ); ?>:
							</label>
						</td>
						<td width="80%">
							<input class="inputbox" type="text" name="emailaddress" id="emailaddress" size="50" value="<?php echo $row->emailaddress;?>" />
						</td>
					</tr>
					<tr>
						<td width="20%" class="key">
							<label for="alias">
								<?php echo JText::_( 'Cell number' ); ?>:
							</label>
						</td>
						<td width="80%">
							<input class="inputbox" type="text" name="cellnumber" id="cellnumber" size="50" value="<?php echo $row->cellnumber;?>" />
						</td>
					</tr>
                    <tr>
						<td width="20%" class="key">
							<label for="alias">
								<?php echo JText::_( 'Province' ); ?>:
							</label>
						</td>
						<td width="80%">
							<?php echo $lists['provinceid']; ?>
						</td>
					</tr>
                    <tr>
						<td width="20%" class="key">
							<label for="alias">
								<?php echo JText::_( 'Club' ); ?>:
							</label>
						</td>
						<td width="80%">
                            <div id="update">
							    <?php echo $lists['clubid']; ?>
                            </div>
                            
						</td>
                        <td>
                            <div id="ajaxLoader" style="left: 550px; top: 400px; width: 50px; position: absolute;  height: 50px">
                                
                            </div>
                        </td>
					</tr>
                    <tr>
						<td width="20%" class="key">
							<label for="alias">
								<?php echo JText::_( 'Teams' ); ?>:
							</label>
						</td>
						<td width="80%">
                            <div id="updateTeamList">
							    <?php echo $lists['teamid']; ?>
                            </div>
                            <?php echo 'Hold ctrl down to select multiple teams.'; ?>
						</td>
					</tr>
                    <tr>
						<td width="20%" class="key">
							<label for="alias">
								<?php echo JText::_( 'Comment' ); ?>:
							</label>
						</td>
						<td width="80%">
							<textarea class="inputbox" cols="70" rows="8" name="comment" id="comment"><?php echo $row->comment;?></textarea>
						</td>
					</tr>
                    <tr>
						<td width="20%" class="key">
							<label for="alias">
								<?php echo JText::_( 'Published' ); ?>:
							</label>
						</td>
						<td width="80%">
							<?php echo $lists['published']; ?>
						</td>
					</tr>
                    <tr>
						<td width="20%" class="key">
							<label for="alias">
								<?php echo JText::_( 'Content ID' ); ?>:
							</label>
						</td>
						<td width="80%">
							<input class="inputbox" type="text" name="contentid" id="contentid" size="50" value="<?php echo $row->contentid;?>" /> (Auto assigned) 
						</td>
					</tr>
                    <tr>
						<td width="20%" class="key">
							<label for="alias">
								<?php echo JText::_( 'User ID' ); ?>:
							</label>
						</td>
						<td width="80%">
							<input class="inputbox" type="text" name="userid" id="userid" size="50" value="<?php echo $row->userid;?>" /> (For future use)
						</td>
					</tr>
                    <tr>
						<td width="20%" class="key">
							<label for="alias">
								
							</label>
						</td>
						<td width="80%">
                            <a href=<?php echo $contentlink; ?>><?php echo JText::_( 'Go to content link' ); ?></a>
						</td>
					</tr>
				</tbody>
				</table>
			</fieldset>
		</div>
		<div class="clr"></div>

		<input type="hidden" name="c" value="player" />
		<input type="hidden" name="option" value="com_statzone" />
		<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
		<input type="hidden" name="task" value="" />
		<?php echo JHTML::_( 'form.token' ); ?>
		</form>
		<?php
        // add mootools
            JHTML::_('behavior.mootools');
        ?>
        <script language="javascript" type="text/javascript">
        window.addEvent('domready', function()
        {
        $('form1').addEvent('submit', function(e)
            {
                // Stop the form from submitting
                new Event(e).stop();
                // Update the page
                //alert("about to send the ajax request");
                //this.send({ update: $('update') });
                $('ajaxLoader').innerHTML = "<img src=\"components/com_statzone/images/ajax-loader.gif\" alt=\"Loading...\" title=\"Loading...\" />";
                this.send({ onComplete: function(response, responseXML)
                {
                    //alert('AJAX Response Received');
                    // get the XML nodes
                    var root = responseXML.documentElement;
                    //alert('is root null:'+root == null);
                    var responseData = root.getElementsByTagName('responsedata').item(0);
                    
                    //the XML should let us know what element to update as well!
                    // prepare the XHTML
                    //alert(responseData == null);
                    var updateValue = responseData.firstChild.nodeValue;
                    
                    if(document.form1.task.value=='updateClublist')
                    {
                        // update the page element 'update'
                        //$('update').empty().setHTML(updateValue);
                        $('update').innerHTML  = updateValue;
                        
                        //now clear list of teams..since the club must first be selected:
                        $('updateTeamList').innerHTML = "<select multiple=\"multiple\" size=\"0\" class=\"inputbox\" id=\"teamid\" name=\"teamid[]\"></select>";
                  
                    }
                    else if(document.form1.task.value=='updateTeamlist')
                    {
                        $('updateTeamList').innerHTML  = updateValue;
                    }
                    else
                    {
                        alert('Unknown task response in Ajax call');
                    }
                    $('ajaxLoader').innerHTML = "";
                    //alert(updateValue);
                }});
            });
        });
        
        function updateParameters()
        {
            var provinceselection = document.adminForm.provinceid.options[document.adminForm.provinceid.options.selectedIndex].value; 
            var clubselection = document.adminForm.clubid.options[document.adminForm.clubid.options.selectedIndex].value; 
            document.form1.provinceid.value = "" + provinceselection;
            document.form1.clubid.value = "" + clubselection;
            //alert("provid sel = " + document.form1.provinceid.value);
            document.form1.id.value = document.adminForm.id.value;
            return true;
        }
       </script>
        <form id="form1" method="post" name="form1" action="<?php 
                $routurl = 'index.php?option=com_statzone#'; 
                echo JRoute::_($routurl); ?>">
                
             
            <input  type="hidden" name="id" type="text" id="id" />
            <input  type="hidden" name="clubid" type="text" id="clubid" />
            <input  type="hidden" name="provinceid" type="text" id="provinceid"/>
            
            <input name="format" type="hidden" id="format" value="raw" />
            <input name="view" type="hidden" id="view" value="wfaq" />
            <div class="clr" ><input name="Submit" type="submit" value="" size="0" onclick="return updateParameters();" /></div>
            
            <input type="hidden" name="c" value="player" />
            <input type="hidden" name="option" value="com_statzone" />
            <input type="hidden" name="task" value="" />
        </form>
		<?php
	}
    
    //
    //View display function to view the list players
    //
    function players( &$rows, &$pageNav, &$lists )
    {
        StatZoneViewPlayers::setPlayersToolbar();
        $user =& JFactory::getUser();
		JHTML::_('behavior.tooltip');
        ?>
		<form action="index.php?option=com_statzone" method="post" name="adminForm">
		<table>
        	<tr>
                <td align="left" width="100%">
                    <?php echo JText::_( 'Filter' ); ?>:
                    <input type="text" name="search" id="search" value="<?php echo $lists['search'];?>" class="text_area" onchange="document.adminForm.submit();" />
                    <button onclick="this.form.submit();"><?php echo JText::_( 'Go' ); ?></button>
                    <button onclick="document.getElementById('search').value='';this.form.getElementById('filter_provid').value='0';this.form.getElementById('filter_clubid').value='0';this.form.getElementById('filter_teamid').value='0';this.form.submit();"><?php echo JText::_( 'Filter Reset' ); ?></button>
                </td>
                <td nowrap="nowrap">
                    <?php
                    echo $lists['filter_provid'];
                    echo $lists['filter_clubid'];
                    echo $lists['filter_teamid'];
                    ?>
                </td>
		    </tr>
		</table>

        <table class="adminlist">
			<thead>
				<tr>
					<th width="2%">
						<?php echo JText::_( 'Num' ); ?>
					</th>
					<th width="2%">
						<input type="checkbox" name="toggle" value=""  onclick="checkAll(<?php echo count( $rows ); ?>);" />
					</th>
					<th nowrap="nowrap" class="title">
						<?php echo JHTML::_('grid.sort',  'First name', 'firstname', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
                    <th nowrap="nowrap" class="title">
						<?php echo JHTML::_('grid.sort',  'Surname', 'surname', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
                    <th nowrap="nowrap" class="title">
						<?php echo JHTML::_('grid.sort',  'Email', 'emailaddress', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
					<th width="10%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'ID Number', 'idnumber', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
                    <th width="10%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Province', 'provincename', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
					<th width="10%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Club', 'clubname', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
                    <th width="10%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Teams', 'clubname', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
                    <th width="1%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Content ID', 'contentid', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
                    <th width="5%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Published', 'showplayer', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
					<th width="1%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'ID', 'id', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<td colspan="13">
						<?php echo $pageNav->getListFooter(); ?>
					</td>
				</tr>
			</tfoot>
			<tbody>
			<?php
            $k = 0;
			for ($i=0, $n=count( $rows ); $i < $n; $i++) {
				$row = &$rows[$i];

				$row->id	= $row->id;
				$link		= JRoute::_( 'index.php?option=com_statzone&c=player&task=edit&cid[]='. $row->id );
                $contentlink		= JRoute::_( 'index.php?option=com_content&sectionid=-1&task=edit&cid[]='. $row->contentid );
                                
				//if( $row->imptotal <= 0 ) {
				//	$row->imptotal	=  JText::_('unlimited');
				//}

                $row->published = $row->showplayer;
				$published		= JHTML::_('grid.published', $row, $i );
				$checked		= JHTML::_('grid.checkedout',   $row, $i );
                
				?>
				<tr class="<?php echo "row$k"; ?>">
					<td align="center">
						<?php echo $pageNav->getRowOffset($i); ?>
					</td>
					<td align="center">
						<?php echo $checked; ?>
					</td>
					<td>
    					<span class="editlinktip hasTip" title="<?php echo JText::_( 'Edit');?>::<?php echo  $row->firstname . ' '. $row->surname; ?>">
                			<a href="<?php echo $link; ?>"><?php echo $row->firstname; ?></a>
						</span>
					</td>
                    <td align="center">
                        <span class="editlinktip hasTip" title="<?php echo JText::_( 'Edit');?>::<?php echo $row->firstname . ' '. $row->surname; ?>">
                			<a href="<?php echo $link; ?>"><?php echo $row->surname; ?></a>
						</span>
						
					</td>
                    <td align="center">
						<?php echo $row->emailaddress;?>
					</td>
					<td align="center">
						<?php echo $row->idnumber;?>
					</td>
					<td align="center">
						<?php echo $row->provincename;?>
					</td>
                    <td align="center">
						<?php echo $row->clubname;?>
					</td>
                     <td align="center">
						<?php 
                        //get a list of teams for use in the grid
                        $db =& JFactory::getDBO();
                        $sql = 'SELECT name as teamname'
                        . ' FROM #__statzone_teams AS t'
                        . ' INNER JOIN #__statzone_playerteams AS pt ON pt.teamid = t.id '
                        . ' WHERE pt.playerid = '. $row->id
                        ;
                        $db->setQuery($sql);
                        if (!$db->query())
                        {
                            $this->setRedirect( 'index.php?option=com_statzone' );
                            return JError::raiseWarning( 500, $db->getErrorMsg() );
                        }
                        $playerteamnames			= $db->loadObjectList() ;
                        foreach($playerteamnames as $teams)
                        {  
                            echo $teams->teamname . '<br>';
                        }
                        ?>
					</td>
                    <td align="center">
						<a href="<?php echo $contentlink;?>">
                            <?php echo $row->contentid;?></a>
					</td>
                    <td align="center">
						<?php echo $published;?>
					</td>
					<td align="center">
						<?php echo $row->id; ?>
					</td>
				</tr>
				<?php
				$k = 1 - $k;
			}
			?>
			</tbody>
			</table>

		<input type="hidden" name="c" value="player" />
		<input type="hidden" name="option" value="com_statzone" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="filter_order" value="<?php echo $lists['order']; ?>" />
		<input type="hidden" name="filter_order_Dir" value="<?php echo $lists['order_Dir']; ?>" />
		<?php echo JHTML::_( 'form.token' ); ?>
		</form>
		<?php
    }
}
